package cn.yunbamboos.media.zlm.client.param;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.yunbamboos.media.exception.MediaException;

import java.util.HashMap;
import java.util.Map;

public class GenericParams implements IParams {

    private final Map<String, String> params = new HashMap<>();

    @Override
    public IParams set(String key, String value, boolean required) {
        if (required && StrUtil.isBlank(value)) {
            throw MediaException.c("{} is required", key);
        }
        if(StrUtil.isNotBlank(value)){
            params.put(key, value);
        }
        return this;
    }

    @Override
    public String toUrl(String path) {
        setParams();
        StringBuilder builder = new StringBuilder(path);
        if(CollectionUtil.isNotEmpty(params)){
            builder.append("?");
            for (Map.Entry<String, String> entry : params.entrySet()) {
                builder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            builder.deleteCharAt(builder.length() - 1);
        }
        return builder.toString();
    }

    protected void setParams(){

    }
}
